<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="体检人员 ID" prop="personId">
        <el-input v-model="queryParams.personId" placeholder="请输入体检人员 ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="有效期" prop="validPeriod">
        <el-input v-model="queryParams.validPeriod" placeholder="请输入有效期" clearable
          @keyup.enter="handleQuery"></el-input>
      </el-form-item>
      <el-form-item label="发证日期" prop="issueDate">
        <el-date-picker clearable v-model="queryParams.issueDate" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择发证日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="过期日期" prop="expirationDate">
        <el-date-picker clearable v-model="queryParams.expirationDate" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择过期日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['health:certificate:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['health:certificate:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['health:certificate:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['health:certificate:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="certificateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="健康证 ID" align="center" prop="certId" />
      <el-table-column label="体检人员 ID" align="center" prop="personId"  />
      <el-table-column label="证书类型" align="center" prop="certType">
        <template #default="scope">
          <span>{{ certTypeMap[scope.row.certType] || '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="validPeriod">
        <template #default="scope">
          <span>{{ scope.row.validPeriod }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发证日期" align="center" prop="issueDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.issueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过期日期" align="center" prop="expirationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="certStatus">
        <template #default="scope">
          <span>{{ certStatusMap[scope.row.certStatus] || '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['health:certificate:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['health:certificate:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改健康证对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="certificateRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="体检人员 ID" prop="personId">
          <el-input v-model="form.personId" placeholder="请输入体检人员 ID" disabled />
        </el-form-item>
        <el-form-item label="有效期" prop="validPeriod">
          <el-input v-model="form.validPeriod" placeholder="请输入有效期" disabled />
        </el-form-item>
        <el-form-item label="发证日期" prop="issueDate">
          <el-date-picker clearable v-model="form.issueDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择发证日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="过期日期" prop="expirationDate">
          <el-date-picker clearable v-model="form.expirationDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择过期日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="certStatus">
          <el-select v-model="form.certStatus" placeholder="请选择状态">
            <el-option label="有效" value="0"></el-option>
            <el-option label="过期
            " value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Certificate">
import { listCertificate, getCertificate, delCertificate, addCertificate, updateCertificate } from "@/api/health/certificate"

const { proxy } = getCurrentInstance()

const certificateList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    personId: null,
    certType: null,
    validPeriod: null,
    issueDate: null,
    expirationDate: null,
    certStatus: null
  },
  rules: {
  }
})

const certTypeMap = {
  '1': '食品行业健康证',
  '2': '公共场所健康证',
  '3': '药品行业健康证'
}

const certStatusMap = {
  '0': '有效',
  '1': '过期'
}

const { queryParams, form, rules } = toRefs(data)

/** 查询健康证列表 */
function getList() {
  loading.value = true
  listCertificate(queryParams.value).then(response => {
    certificateList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    certId: null,
    personId: null,
    certType: null,
    validPeriod: null,
    issueDate: null,
    expirationDate: null,
    certStatus: null
  }
  proxy.resetForm("certificateRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.certId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加健康证"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _certId = row.certId || ids.value
  getCertificate(_certId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改健康证"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["certificateRef"].validate(valid => {
    if (valid) {
      if (form.value.certId != null) {
        updateCertificate(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCertificate(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _certIds = row.certId || ids.value
  proxy.$modal.confirm('是否确认删除健康证编号为"' + _certIds + '"的数据项？').then(function () {
    return delCertificate(_certIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('health/certificate/export', {
    ...queryParams.value
  }, `certificate_${new Date().getTime()}.xlsx`)
}

getList()
</script>
